package com.main.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.generico.dao.ctg.CtgCatalogoDao;

import com.generico.exception.AsiWebException;

import com.main.dao.DetalleRequesicionDao;

import com.main.dao.RequisicionDao;

import com.main.dao.UserDAO;

import com.main.java.DetalleRequisicion;

import com.main.java.Requisicion;

import com.web.util.GenericoUtil;

@Controller
@RequestMapping("/empleado")
public class requisicionInventarioController extends BaseController{

	
	
	@Autowired
	private RequisicionDao requisicionDao;
	
	@Autowired
	private UserDAO userDAO;
	
	@Autowired
	private DetalleRequesicionDao detalleRequesicionDao;
	
	@Autowired
	private CtgCatalogoDao ctgCatalogoDao;
	
	
	@RequestMapping(value = "/requisicionInventario/View")
	public String requisicionInventario(HttpServletRequest request, ModelMap model){
		List<Object[]> materiaPrima = ctgCatalogoDao.findAllActivesAsArray(GenericoUtil.CTG_MATERIAL_ALFOMBRA);
		materiaPrima.addAll(ctgCatalogoDao.findAllActivesAsArray(GenericoUtil.CTG_MATERIAL_CORTINA));
		model.put("materiaPrima", JSONArray.fromObject(materiaPrima));
		model.put("userName", GenericoUtil.getUserName());
		model.put("usuarioId", GenericoUtil.getSgdUsuarioIdFromAcegi());
		model.put("rol", GenericoUtil.getRol().toString());
		return "empleado/requisicionInventario";
	}
	
	

	
	
	
	
	@RequestMapping(value = "/guardarRequisicion", method = RequestMethod.POST)
	public void guardarRequisicion(HttpServletResponse response,
			@RequestParam(value = "cantidad") String cantidad,
			@RequestParam(value = "materiaPrimaId") String materiaPrimaId,
			@RequestParam(value = "detalle") String detalle,
			@RequestParam(value = "clienteId") Long clienteId){
		Map<String, Object> result = new HashMap<String, Object>();
//
		try{
			if(cantidad.isEmpty()&& materiaPrimaId.isEmpty()){
				result.put(FAILURE, true);
				result.put(MESSAGE, "Debe elegir Materia Prima para poder continuar");
			}else{
				
				Requisicion requisicion = new Requisicion();
				requisicion.setRequisicionFecha(GenericoUtil.dateFormatAsDD_MM_YYYY___HH_MM_SS_A.format(new Date()));
				requisicion.setRequisicionObservacion("prueba");
				requisicion.setRequisicionEstado(false);
				requisicion.setUsuario(userDAO.getUserById(GenericoUtil.getSgdUsuarioIdFromAcegi()));
				
				requisicionDao.guardarRequisicion(requisicion);
				
				
//				-------------------------------------------------------------------------------------------
				
				
				
				
				String[] detalleArray = detalle.split("~");
				String[] cantidadArray = cantidad.split("~");
				String[] materiaPrimaIdArray = materiaPrimaId.split("~");
				
				for (int i = 0; i < detalleArray.length; i++) {
					DetalleRequisicion detalleRequisicion = new DetalleRequisicion();
					detalleRequisicion.setRequesicionDetalle(detalleArray[i]);
					detalleRequisicion.setRequisicionDetalleCantidad(cantidadArray[i]);
					detalleRequisicion.setCtgMateriaPrima(ctgCatalogoDao.findByCtgCatalogoId(Long.parseLong(materiaPrimaIdArray[i])));;
					detalleRequisicion.setRequisicion(requisicion);
					detalleRequesicionDao.guardarDetalleRequisicion(detalleRequisicion);
					
				}
				
				
				
				
				
				
				
				

				result.put(MESSAGE, "Requisicion puesta en cola!");
				result.put(SUCCESS, true);
				
			}

		} catch (AsiWebException e) {
			result.put(FAILURE, true);
			result.put(MESSAGE, GenericoUtil.getExceptionMessage(e));
			logger.error(e, e);
		} catch (Exception e) {
			result.put(FAILURE, true);
			result.put(MESSAGE, GenericoUtil.ERROR_MESSAGE);
			logger.error(e, e);
		}
		serializeObject(result, response);
	}
	
	
	
	
	
}
	
	
	
	
	

